package yingke.com.yingke.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.avos.avoscloud.RequestMobileCodeCallback;

import yingke.com.yingke.R;

import static yingke.com.yingke.MyApp.loginState;

public class LoginMoblieActivity extends AppCompatActivity implements View.OnClickListener {

    private Toolbar login_mobile_tb;
    private EditText mobile_num;
    private EditText mobile_code;
    private String code;
    private String num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login_moblie);
        iniView();
    }

    private void iniView() {
        login_mobile_tb = (Toolbar) findViewById(R.id.login_mobile_tb);
        mobile_code = (EditText) findViewById(R.id.login_mobile_code_et);
        mobile_num = (EditText) findViewById(R.id.login_mobile_num_et);
        findViewById(R.id.login_mobile_login_bt).setOnClickListener(this);
        findViewById(R.id.login_mobile_submit_bt).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_mobile_submit_bt://发送验证码
                num = mobile_num.getText().toString();
                AVOSCloud.requestSMSCodeInBackground(num, new RequestMobileCodeCallback() {
                    @Override
                    public void done(AVException e) {
                        if(e==null){
                            Toast.makeText(LoginMoblieActivity.this, "验证码发送成功", Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(LoginMoblieActivity.this, "验证码发送失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
            case R.id.login_mobile_login_bt://验证登录
                code = mobile_code.getText().toString();
                AVUser.signUpOrLoginByMobilePhoneInBackground(num, code, new LogInCallback<AVUser>() {
                    @Override
                    public void done(AVUser avUser, AVException e) {
                        if(e==null){
                            Toast.makeText(LoginMoblieActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            loginState=true;
                            AVUser.getCurrentUser().put("signature", "好像忘记签名了");
                            AVUser.getCurrentUser().saveInBackground();
                            AVUser.getCurrentUser().put("nickname", "用户"+num);
                            AVUser.getCurrentUser().saveInBackground();
                            startActivity(new Intent(LoginMoblieActivity.this, MainActivity.class));
                            finish();
                        }else {
                            Toast.makeText(LoginMoblieActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
        }
    }
}
